package com.hqbzl.c1.cass;

import lombok.ToString;

import java.util.concurrent.atomic.*;

public class TestAtomicMarkableReference {
    public static void test1() {
        // 与 TestAtomicStamperReference的区，一个只关心结果要，是不是变化了，一个是根据版本号
        final AtomicMarkableReference<String> stringAtomicMarkableReference = new AtomicMarkableReference<String>("A", true);
        final boolean marked = stringAtomicMarkableReference.isMarked();
        stringAtomicMarkableReference.compareAndSet("A", "B", marked, !marked);
        System.out.println("stringAtomicMarkableReference.isMarked() = " + stringAtomicMarkableReference.isMarked());
        System.out.println("stringAtomicMarkableReference.getReference() = " + stringAtomicMarkableReference.getReference());

    }

    public static void main(String[] args) {
        test1();
    }

    public static void main1(String[] args) {

        // 原子数组
        final AtomicIntegerArray arr1 = new AtomicIntegerArray(10);
        //AtomicReferenceFieldUpdater
        //AtomicIntegerFieldUpdater
        //AtomicLongFieldUpdater
        Student stu = new Student();
        // 对象属性赋值的元子性
        final AtomicReferenceFieldUpdater<Student, String> updater = AtomicReferenceFieldUpdater.newUpdater(Student.class, String.class, "name");
        System.out.println(updater.compareAndSet(stu, null, "lisi"));
    }

    static class Student {
        private volatile String name;

        @Override
        public String toString() {
            return super.toString();
        }
    }
}

